﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Markup;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace AutomationMLMapper.Controls.MarkupExtensions
{
    /// <summary>
    /// Simple extension for ImageSource, let you choose icon with specific size.
    /// Usage sample: Image Stretch="None" Source="{common:Icon /ControlsTester;component/icons/custom-reports.ico, 16}"
    /// Or: Image Source="{common:Icon Source={Binding IconResource},Size=16} "
    /// </summary> 
    public class ImageResolutionExtension : MarkupExtension
    {
        private string source;

        public string Source
        {
            get
            {
                return this.source;
            }

            set
            {
                // Have to make full pack URI from short form, so System.Uri can regognize it.
                this.source = "pack://application:,,,/AutomationMLMapper;component/Images/" + value;
            }
        }

        public double Size { get; set; }

        
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            var host = target.TargetObject as FrameworkElement;

            if ((Size <= 0 || double.IsNaN(Size)) && host != null)
            { Size = host.Width; }

            var decoder = BitmapDecoder.Create(new Uri(this.Source), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);

            var result = decoder.Frames.SingleOrDefault(f => f.Width == this.Size);
            if (result == default(BitmapFrame))
                result = decoder.Frames.OrderBy(f => f.Width).First();

            var prop = target.TargetProperty as DependencyProperty;
            //If we aren't sure they want a ImageSource, lets give a
            //full blown image.
            //if (prop == null || prop.PropertyType != typeof(ImageSource))
            //{
            //    var img = new Image();
            //    img.Stretch = Stretch.None;
            //    img.Source = result;
            //    return img;
            //}
            return result;
        }

        public BitmapFrame GetResolutionImage()
        {           
            var decoder = BitmapDecoder.Create(new Uri(this.Source), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);

            var result = decoder.Frames.SingleOrDefault(f => f.Width == this.Size);
            if (result == default(BitmapFrame))
                result = decoder.Frames.OrderBy(f => f.Width).First();

            return result;
        }

        public ImageResolutionExtension(string source, int size)
        {
            this.Source = source;
            this.Size = size;
        }

        public ImageResolutionExtension() { }
    }

}
